home *** CD-ROM | disk | FTP | other *** search
/ Plug-In Power Pack for Netscape Communicator / Plug-In Power Pack for Netscape Communicator.iso / plugins / dataviews / dvdraw / examples / datasource / parser.cpp < prev    next >
C/C++ Source or Header  |  1997-05-23  |  4KB  |  183 lines

  1. // Parser.cpp : Code that reads the variable defintion file.  This code is
  2. //              segregated for simply pedantic purposes in that it might
  3. //              detract a user from understanding the real purpose of this
  4. //              example, which is to show how a data source browser is expected
  5. //              to interface with DV-Draw (and not how to parse a data file!).
  6. //
  7.  
  8. #include "stdafx.h"
  9. #include "ExampleDsDll.h"
  10. #include "VUfundecl.h"
  11.  
  12. #define READ_BUFSIZE        256
  13. #define COMMENT_SYMBOL      '#'
  14. #define PLACE_HOLDER        '*'
  15.  
  16.  
  17. #define SKIPSPACES(x) for( ; (*x == ' ') || (*x == '\t'); (x)++)
  18. #define SKIPTOSPACE(x) for( ; (*x != ' ') && (*x != NULL) && (*x != '\t'); (x)++)
  19. #define LINE_OK(x)     ( *x != COMMENT_SYMBOL && *x != '\n' )
  20.  
  21.  
  22. ////////////////////////////////////////////////////////////////////////////////
  23. //
  24. //  OpenDataFile
  25. //
  26. BOOL CExampleDsDll::OpenDataFile(CStdioFile& cFile)
  27. {
  28.     char *dvHome = 0;
  29.  
  30.     if( (VURegQueryDvHome(&dvHome)) == ERROR_SUCCESS)
  31.     {
  32.         CString csSuffix("\\DVdraw\\examples\\DataSource\\VarDefs.dat");
  33.         CString csVarFile = dvHome;
  34.         csVarFile += csSuffix;
  35.  
  36.         free(dvHome);
  37.  
  38.         return cFile.Open((LPCTSTR)csVarFile, CFile::modeRead);
  39.     }
  40.  
  41.     return FALSE;
  42. }
  43.  
  44.  
  45. ////////////////////////////////////////////////////////////////////////////////
  46. //
  47. //  LookupBaseAddr
  48. //
  49. //    Since its such a small number of types, we'll just brute force it...
  50. //
  51. ADDRESS CExampleDsDll::LookupBaseAddr(int data_type)
  52. {
  53.   for(int i =0;i<NUM_DATA_TYPES && data_type != m_TypeTable[i].type;i++);
  54.   return(m_TypeTable[i].addr);
  55. }
  56.  
  57.  
  58. ////////////////////////////////////////////////////////////////////////////////
  59. //
  60. //  LookupDataType
  61. //
  62. //    Since its such a small number of types, we'll just brute force it...
  63. //
  64. char* CExampleDsDll::LookupDataType(char* string, int* type )
  65. {
  66.     for(int i = 0;i<NUM_DATA_TYPES && strcmp(string,m_TypeTable[i].name);i++);
  67.     *type = m_TypeTable[i].type;
  68.     return( m_TypeTable[i].name2 );
  69. }
  70.  
  71.  
  72. ////////////////////////////////////////////////////////////////////////////////
  73. //
  74. //  GetNextWord
  75. //
  76. int CExampleDsDll::GetNextWord(char** start, char** end,  BOOL term)
  77. {
  78.     int len;
  79.  
  80.     SKIPSPACES(*start);
  81.     *end = *start;
  82.     SKIPTOSPACE(*end);
  83.     len = (int)(*end - *start);
  84.     if( *end && term )
  85.     {
  86.         **end = '\0';
  87.         (*end)++;
  88.     }
  89.     return( len );
  90. }
  91.  
  92.  
  93. /////////////////////////////////////////////////////////////////////////////
  94. //
  95. //  ReadVarDefinitions
  96. //
  97. void CExampleDsDll::ReadVarDefinitions()
  98. {
  99.     char        *start, *end, buffer[READ_BUFSIZE];
  100.     CString        varName;
  101.     CStdioFile    cFile;
  102.     VARDEFDATA* pLastEntry = 0;
  103.  
  104.     if(!OpenDataFile(cFile))
  105.         return;
  106.  
  107.     while(cFile.ReadString(buffer, READ_BUFSIZE))
  108.     {
  109.         VARDEFDATA* pData = 0;
  110.  
  111.         start = buffer;
  112.  
  113.         SKIPSPACES(start);
  114.  
  115.         if( LINE_OK(start) )
  116.         {
  117.             // Get variable's name and allocate buffer space
  118.             GetNextWord( &start, &end, YES );
  119.             varName = start;
  120.  
  121.             pData = new VARDEFDATA;
  122.             pData->next = 0;
  123.  
  124.             // Set the head of the list
  125.             if(!m_pVarDefList)
  126.                 m_pVarDefList = pData;
  127.  
  128.             // Insert new entry at end of list
  129.             if(pLastEntry)
  130.                 pLastEntry->next = pData;
  131.  
  132.             // Remember this new entry as the last one...
  133.             pLastEntry = pData;
  134.  
  135.             // Get variable type
  136.             start = end;
  137.             GetNextWord( &start, &end, YES );
  138.             pData->m_csVarTypeName = LookupDataType(start, &(pData->m_nVarType));
  139.  
  140.             // Local data buffer we want to use...
  141.             pData->m_pBuffer = LookupBaseAddr(pData->m_nVarType);
  142.  
  143.             // Get number of rows...
  144.             start = end;
  145.             GetNextWord( &start, &end, YES );
  146.             if( start[0] == PLACE_HOLDER )
  147.                 pData->m_nRows = 1;
  148.             else
  149.                 pData->m_nRows = max(1, atoi(start));
  150.  
  151.             // ...and columns.
  152.             start = end;
  153.             GetNextWord( &start, &end, YES );
  154.             if( start[0] == PLACE_HOLDER )
  155.                 pData->m_nCols = 1;
  156.             else
  157.                 pData->m_nCols = max(1, atoi(start));
  158.  
  159.             // Get low value...
  160.             start = end;
  161.             GetNextWord( &start, &end, YES );
  162.  
  163.             if( start[0] == PLACE_HOLDER )
  164.                 pData->m_dLowRange = 0.0;
  165.             else
  166.                 pData->m_dLowRange = atof( start ); 
  167.  
  168.             // ...and high value
  169.             start = end;
  170.             GetNextWord( &start, &end, YES );
  171.             if( start[0] == PLACE_HOLDER )
  172.                 pData->m_dHighRange = 1.0;
  173.             else
  174.                 pData->m_dHighRange = atof( start );
  175.  
  176.             SetVarTagName(pData, (LPCTSTR)varName);
  177.         }
  178.     }
  179.  
  180.     cFile.Close();
  181. }
  182.  
  183.